/**
 * Created by Mark_ren on 2016/11/28.
 * 苟富贵,無相忘
 */
var moment = require("moment");
var co = require('co');
var seqModule = require('../../../routes/createModules');
var seq = new seqModule();
var formidable = require('formidable');
var fs = require('fs');
var util = require('../../../common/util');


var qdWjController = {
    getQdWjPage: function (req, res) {
        co(function () {
            var params = {};
            var paramT = {};
            if (req.query.meetId != undefined && req.query.meetId != null && req.query.meetId != "") {
                params["meetId"] = req.query.meetId;
            } else {
                params["$or"] = [{ "meetId": null }, { "meetId": "" }];
            }
            if (req.query.wjType != undefined) {
                params.wjType = parseInt(req.query.wjType);
            }
            if (req.query.title != undefined) {
                params.title = { "$like": "%" + req.query.title + "%" };
            }
            if (req.query.state != undefined) {
                params.state = parseInt(req.query.state);
            }
            paramT.where = params;
            paramT.order = [["createTime", "DESC"]];
            var page = parseInt(req.query.page);
            if (req.query.page != undefined && req.query.rows != undefined) {
                paramT["offset"] = parseInt(req.query.page);
                paramT["limit"] = parseInt(req.query.rows);
            }
            seq.qdWjModule.findAndCountAll(paramT).then(function (result) {
                res.json({
                    'success': true, 'msg': '查询成功', 'page': page, 'total': result.count, 'result': result.rows
                });
            })

        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    },
    getWjById: function (req, res) {
        co(function () {
            var paramT = {};
            paramT.where = req.params;
            seq.qdWjModule.findOne(paramT).then(function (result) {
                res.json({ 'success': true, 'msg': '查询成功', 'result': result });
            })

        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })
    },
    addQdWj: function (req, res) {
        co(function () {
            if (req.method.toLowerCase() == 'post') {
                // parse a file upload
                var form = new formidable.IncomingForm();
                //files = [],
                //fields = [];
                form.uploadDir = "public/data/qrImage";
                form.type = true;

                form.on("progress", function (bytesReceived, bytesExpected) {
                    console.log("progress!" + bytesReceived + " B____" + (bytesExpected / 1024 / 1024).toFixed(2) + " MB");
                    // 百分比
                    var percent = Math.round(bytesReceived / bytesExpected * 100);
                    console.log("percent:" + percent + "%");
                    // 存入
                    //sessions.setSession(req,res,opts);
                });
                form.parse(req, function (err, fields, files) {
                    // res.render('index', { paths: [files.upload.name] });

                    if (files.wjPic != null && files.wjPic.name != "") {

                        var imgType = files.wjPic.name.split(".");
                        files.wjPic.name = "wjImage" + moment().format("YYYYMMDDHHmmss") + "." + imgType[imgType.length - 1];
                        fs.renameSync(files.wjPic.path, form.uploadDir + "/" + files.wjPic.name);
                        fields["wjPic"] = files.wjPic.name;
                    }

                    if (files.resultPic != null && files.resultPic.name != "") {

                        var imgType = files.resultPic.name.split(".");
                        files.resultPic.name = "resultImage" + moment().format("YYYYMMDDHHmmss") + "." + imgType[imgType.length - 1];
                        fs.renameSync(files.resultPic.path, form.uploadDir + "/" + files.resultPic.name);
                        fields["resultPic"] = files.resultPic.name;
                    }
                    fields.createTime = util.getNowTime();
                    seq.qdWjModule.create(fields).then(function (qdMeet) {
                        res.redirect(util.getIP() + "qdWj/qdWjList.html?meetId=" + fields.meetId);
                    })

                });
            }
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '添加失败' });
            console.log(e);
        })

    },
    updateQdWj: function (req, res) {
        co(function () {
            if (req.method.toLowerCase() == 'post') {
                // parse a file upload
                var form = new formidable.IncomingForm();
                //files = [],
                //fields = [];
                form.uploadDir = "public/data/qrImage";
                form.type = true;

                form.on("progress", function (bytesReceived, bytesExpected) {
                    console.log("progress!" + bytesReceived + " B____" + (bytesExpected / 1024 / 1024).toFixed(2) + " MB");
                    // 百分比
                    var percent = Math.round(bytesReceived / bytesExpected * 100);
                    console.log("percent:" + percent + "%");
                    // 存入
                    //sessions.setSession(req,res,opts);
                });
                form.parse(req, function (err, fields, files) {
                    // res.render('index', { paths: [files.upload.name] });

                    if (files.wjPic != null && files.wjPic.name != "") {

                        var imgType = files.wjPic.name.split(".");
                        files.wjPic.name = "wjImage" + moment().format("YYYYMMDDHHmmss") + "." + imgType[imgType.length - 1];
                        fs.renameSync(files.wjPic.path, form.uploadDir + "/" + files.wjPic.name);
                        fields["wjPic"] = files.wjPic.name;
                    }

                    if (files.resultPic != null && files.resultPic.name != "") {

                        var imgType = files.resultPic.name.split(".");
                        files.resultPic.name = "resultImage" + moment().format("YYYYMMDDHHmmss") + "." + imgType[imgType.length - 1];
                        fs.renameSync(files.resultPic.path, form.uploadDir + "/" + files.resultPic.name);
                        fields["resultPic"] = files.resultPic.name;
                    }
                    seq.qdWjModule.update(fields, { where: req.params }).then(function (qdMeet) {
                        res.redirect(util.getIP() + "qdWj/qdWjList.html?meetId=" + fields.meetId);
                    })
                });
            }
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '修改失败' });
            console.log(e);
        })
    },
    deleteQdWj: function (req, res) {
        co(function () {
            var params = req.query.params;
            var arr = params.split(",");
            var paramT = [arr];
            seq.qdWjModule.destroy({ where: { id: paramT } }).then(function (qdMeet) {
                res.json({ 'success': true, 'msg': '删除成功' });
            })
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '删除失败' });
            console.log(e);
        })
    }


}
module.exports = qdWjController;